import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/utils/xindong_calender_util.dart';
import 'package:xindong/utils/xindong_text_util.dart';
import 'package:xindong/widgets/common/xindong_app_bar_icon_button.dart';
import 'package:xindong/widgets/common/xindong_app_bar_dialog.dart';
import 'package:xindong/widgets/dialog/xindong_base_sheet.dart';

class XinDongBirthdaySheet extends StatelessWidget {
  final String? selectedDateText; // 格式：1970-01-01
  final Function(String dateText) doneCallback;
  final bool isMan;

  XinDongBirthdaySheet.show({super.key, this.selectedDateText, required this.doneCallback, required this.isMan}) {
    showSmartSheet(sheet: this);
  }

  @override
  Widget build(BuildContext context) {
    DateTime? currentDate;
    String birthday = "";
    if (TextUtil.isEmpty(selectedDateText)) {
      if (isMan) {
        int number = Random().nextInt(10);
        currentDate = XinDongCalendarUtil.yearsAgo(20 + number);
      } else {
        currentDate = XinDongCalendarUtil.yearsAgo(20);
      }
      birthday = XinDongCalendarUtil.dateFormatDateTime(currentDate, format: XinDongDateFormat.formatYMD);
    } else {
      currentDate = XinDongCalendarUtil.dateTimeFromString(selectedDateText!);
      birthday = selectedDateText!;
    }
    return Container(
      color: DZColors.dialog,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          XinDongDialogAppBar(
            '选择生日',
            leading: XinDongAppBarIconButton(onTap: () => SmartDialog.dismiss()),
            onTapDone: () {
              doneCallback(birthday);
              SmartDialog.dismiss();
            },
          ),
          SizedBox(
            height: 216 + kAppBarHeight,
            child: CupertinoTheme(
              data: const CupertinoThemeData(
                textTheme: CupertinoTextThemeData(
                  dateTimePickerTextStyle: TextStyle(color: DZColors.titleColor, fontWeight: FontWeight.normal, fontSize: 20),
                ),
              ),
              child: CupertinoDatePicker(
                // 模式
                mode: CupertinoDatePickerMode.date,
                // 当前日期
                initialDateTime: currentDate,
                // 最小18岁
                maximumDate: XinDongCalendarUtil.yearsAgo(18),
                // 最大68岁
                minimumDate: XinDongCalendarUtil.yearsAgo(65),
                // 排序
                dateOrder: DatePickerDateOrder.ymd,
                onDateTimeChanged: (DateTime dateTime) {
                  birthday = XinDongCalendarUtil.dateFormatDateTime(dateTime, format: XinDongDateFormat.formatYMD);
                },
              ),
            ),
          ),
        ],
      ),
    );
  }
}
